package com.kuzan.autocloud.common.model.organization;

import com.kuzan.autocloud.common.model.Base;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;

/**
 * <p>Description: 组织</p>
 *
 * @author kuzank
 */
@Data
@NoArgsConstructor
@Entity
@Table(name = "sys_organization")
public class Organization extends Base {

    @Column(length = 32)
    private String pid;
    @Lob
    @Column(columnDefinition = "TEXT")
    private String idpath;
    @Column(length = 32)
    private String chargePerson;
    @Column(length = 32)
    private String avatar;


    public static Organization ofRoot(String title, String id, String chargePerson) {
        return of(title, id, null, id, chargePerson);
    }

    public static Organization of(String title, String id, String pid, String idpath, String chargePerson) {
        Organization organization = new Organization();
        organization.setTitle(title);
        organization.setId(id);
        organization.setChargePerson(chargePerson);
        organization.setPid(pid);
        organization.setIdpath(idpath);
        organization.setSort(getSortIncrement());
        return organization;
    }

    public static Organization ofParent(String title, String id, Organization parent, String chargeaccount) {
        String idpath = parent.getIdpath() == null || parent.getIdpath().length() == 0 ? parent.getId() + "," + id : parent.getIdpath() + "," + id;
        return of(title, id, parent.getId(), idpath, chargeaccount);
    }
}
